﻿using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Text;
using System.Xml.Serialization;

namespace Brain
{
    /// <summary>
    /// Class responsible for Serializing / Deserializing with XML
    /// </summary>
    public static class XMLWriter
    {
        /// <summary>
        /// Serializes a List of Data into a XML document.
        /// </summary>
        /// <param name="Data">The Data list to serialize</param>
        public static void Write(List<SerializationData> Data, Stream stream)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            XmlWriter writer = XmlWriter.Create(stream, settings);
            writer.WriteStartDocument();
            writer.WriteStartElement("ArrayOfSerializationData");
            for (int i = 0; i < Data.Count; i++)
            {
                SerializationData data = Data[i];

                writer.WriteStartElement("object");

                writer.WriteElementString("ParentType", data.TypeName.ToString());

                for(int x = 0; x < data.Data.Count; x++)
                {
                    SerializationDataObject obj = data.Data[x];
                    if (obj.Object == null)
                    {
                        writer.WriteElementString(obj.Key, "null");
                    }
                    else
                    {
                        writer.WriteElementString(obj.Key, obj.Object.ToString());
                    }
                }

                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();

            writer.Flush();
            writer.Close();
        }

        /// <summary>
        /// Deserializes the XML document from the stream
        /// </summary>
        /// <param name="Manager">The Serialization Manager deserializing</param>
        /// <param name="stream">Stream that contains the XML Document.</param>
        /// <param name="DataList">The List to deserialize to</param>
        public static List<SerializationData> Read(Stream stream)
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Auto;

            XmlReader reader = XmlReader.Create(stream, settings);

            List<SerializationData> DataList = new List<SerializationData>();

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "ArrayOfSerializationData")
                    {
                        while (reader.Read() && reader.NodeType != XmlNodeType.EndElement)
                        {
                            if (reader.Name == "object")
                            {
                                SerializationData dataList = new SerializationData();
                                reader.Read();

                                while (reader.Read() && reader.NodeType != XmlNodeType.Whitespace &&
                                    reader.NodeType != XmlNodeType.EndElement)
                                {
                                    dataList.AddData(reader.Name.ToString(), reader.ReadElementContentAsString());
                                }
                                DataList.Add(dataList);
                            }
                        }
                    }
                }
            }

            return DataList;
        }
    }
}
